/*
 * Copyright (c) 2006-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import org.panteleyev.swing.AboutSystemPanel;
import org.panteleyev.swing.BasicDialog;
import org.panteleyev.swing.SwingFactory;
import org.panteleyev.tools.About3rdPartyPanel;

public class AboutDialog extends BasicDialog<BloggerFrame<?>> {
    public final static String PRODUCT_NAME = "Petrus Blogger";
    public final static String VERSION = "20110808";
    public final static String HOMEPAGE = "http://www.petrus-blogger.org/";

    private About3rdPartyPanel creditsPanel;
    private JPanel systemPanel;

    private AbstractAction creditsAction;
    private JButton crButton;

    /**
     * Creates new form AboutDialog
     * @param parent
     */
    public AboutDialog(BloggerFrame<?> parent) {
        super(parent);
        setTitle(L10N.ABOUT_DIALOG_TITLE.s());

        createActions();

        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        JPanel productPanel;

        SwingFactory.add(getContentPane(),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                SwingFactory.createPanel(BorderFactory.createEmptyBorder(10, 10, 10, 10), SwingFactory.LAYOUT.FLOW,
                    new JLabel(Icon.BIG_LOGO.i())
                ),
                productPanel = SwingFactory.createPanel(BorderFactory.createEmptyBorder(10, 10, 10, 10), SwingFactory.LAYOUT.BOX_Y,
                    new JLabel("<html><b><font size='+2'>" + PRODUCT_NAME + "</font></b></html>"),
                    Box.createVerticalStrut(10),
                    new JLabel("<html>&#169; 2006-2011 Petr Panteleyev</html>"),
                    new JLabel("All rights reserved."),
                    Box.createVerticalStrut(10),
                    SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                        new JLabel(L10N.ABOUT_DIALOG_VERSION_LABEL.s()),
                        Box.createHorizontalStrut(3),
                        new JLabel(VERSION)
                    )
                )
            ),
            SwingFactory.createPanel(BorderFactory.createEmptyBorder(10, 10, 10, 10), SwingFactory.LAYOUT.FLOW,
                systemPanel = new AboutSystemPanel(BorderFactory.createLineBorder(Color.BLACK))
            ),
            creditsPanel = new About3rdPartyPanel(BorderFactory.createEmptyBorder(10, 10, 10, 10))
                .addCopyright("<b>Google GData 1.0</b><br>Copyright &copy; 2006 Google Inc.<br>Licensed under the Apache License, Version 2.0")
                .addCopyright("<b>XML RPC 3.1</b><br>Copyright &copy; 2001-2007 The Apache Software Foundation<br>Licensed under the Apache License, Version 2.0")
                .addCopyright("<b>SwingX 1.0</b><br>Copyright &copy; 2005-2006 Sun Microsystems, Inc.<br>Licensed under GNU LGPL, Version 2.1")
                .addCopyright("<b>Typograf</b><br>Copyright &copy; 1995-2008 Art. Lebedev Studio")
                .addCopyright("<b>" + PRODUCT_NAME + " Logo</b> made from photo located at<br>http://www.flickr.com/photo_zoom.gne?id=60277086&size=o<br>Released under Creative Commons license"),
            SwingFactory.createPanel(BorderFactory.createEmptyBorder(10, 10, 10, 10), SwingFactory.LAYOUT.BOX_X,
                crButton = new JButton(creditsAction),
                Box.createHorizontalGlue(),
                new JButton(new AbstractAction(L10N.ABOUT_DIALOG_CLOSE_BUTTON.s()) {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                })
            )
        );

        SwingFactory.setAlignmentX(productPanel, LEFT_ALIGNMENT);
        creditsPanel.setVisible(false);
        pack();

        installEscapeHandler();
    }

    private void createActions() {
        creditsAction = new AbstractAction(L10N.ABOUT_DIALOG_CREDITS_BUTTON.s()) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (systemPanel.isVisible()) {
                    creditsPanel.setVisible(true);
                    systemPanel.setVisible(false);
                    crButton.setText(L10N.ABOUT_DIALOG_SYSTEM_BUTTON.s());
                } else {
                    creditsPanel.setVisible(false);
                    systemPanel.setVisible(true);
                    crButton.setText(L10N.ABOUT_DIALOG_CREDITS_BUTTON.s());
                }
                pack();
                center();
            }
        };
    }

    private void installEscapeHandler() {
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                setVisible(false);
            }
        };

        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        getRootPane().registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

}
